<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>数组算法-改变</title>
</head>
<body>
<script>
console.log('----------------------reverse----------------------');
//颠倒数组中元素的顺序
const arr01 = [4, 5, 6, 7];
arr01.reverse();
console.log('arr01:', arr01);//[7, 6, 5, 4]
</script>
<script>
console.log('----------------------sort----------------------');
//对数组的元素进行排序
const arr02 = [4, 5, 6, 7];
arr02.sort((next, cur) => {
	return next - cur;
});
console.log('arr02:', arr02);//[7, 6, 5, 4]
</script>
<script>
console.log('----------------------includes----------------------');
//判断一个数组是否包含一个指定的值 arr.includes(searchElement, fromIndex)
const arr03 = [4, 5, 6, 7];
const res031 = arr03.includes(5);
console.log('res031:', res031);//true
const res032 = arr03.includes(5, 4);//从下标为4开始找
console.log('res032:', res032);//false
</script>
<script>
console.log('----------------------indexOf----------------------');
//可返回数组中某个指定的元素位置 array.indexOf(item,start)
const arr04 = [4, 5, 6, 7, 6];
const res04 = arr04.indexOf(6);
console.log('res04:', res04);//2
</script>
<script>
console.log('----------------------lastIndexOf----------------------');
//返回一个指定的元素在数组中最后出现的位置 array.lastIndexOf(item,start)
const arr05 = [4, 5, 6, 7, 6];
const res05 = arr04.lastIndexOf(6);
console.log('res05:', res05);//4
</script>
<script>
console.log('----------------------reduce----------------------');
//array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
//接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值
const reduceArr1 = [2, 3, 5, 6];
const reduceRes1 = reduceArr1.reduce((total, currentValue, currentIndex, arr) => {
	console.log('total:', total);//2, 8, 18
	return total + currentValue * 2;
});
console.log('reduceRes1:', reduceRes1);//30
</script>
<script>
console.log('----------------------reduceRight----------------------');
//array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
//接收一个函数作为累加器，数组中的每个值（从左到右）开始缩减，最终计算为一个值
const reduceRightArr1 = [2, 3, 5, 6];
const reduceRightRes1 = reduceArr1.reduceRight((total, currentValue, currentIndex, arr) => {
	console.log('total:', total);//6, 16, 22
	return total + currentValue * 2;
});
console.log('reduceRightRes1:', reduceRightRes1);//26
</script>
</body>
</html>